package com.ruoyi.activiti.controller;


import com.ruoyi.activiti.entity.fact.AddressCheckResult;
import com.ruoyi.activiti.entity.fact.AddressRule;
import com.ruoyi.activiti.service.DroolsRulesService;
import com.ruoyi.common.core.dto.order.OrderDto;
import org.apache.poi.ss.formula.functions.Address;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.ruoyi.common.core.context.SecurityContextHolder.THREAD_LOCAL;

@RestController
public class OrderDroolsController {
    @Autowired
    private KieBase kieBase;
    @Autowired
    DroolsRulesService droolsRulesService;

    @PostMapping("/drools")
    public String orderDrools(@RequestBody OrderDto orderDto){
        AddressRule addressRule = new AddressRule();
        addressRule.setTotalWeight(orderDto.getWeight());
        addressRule.setDistance(orderDto.getDistance());
        KieSession session = kieBase.newKieSession();
        session.insert(addressRule);

        AddressCheckResult addressCheckResult = new AddressCheckResult();
        session.insert(addressCheckResult);
        int i = session.fireAllRules();
        System.out.println("触发的规则数量："+i);

        session.destroy();
        if(addressCheckResult.isPostCodeResult()){
            System.out.println("规则匹配成功 结果为"+ addressCheckResult.getResult());
            return addressCheckResult.getResult();
        }
        return "0";
    }
}
